package com.score;

/**
 * Created by Roger on 2018/9/20.
 */

public class ByteUtils {
    public static int byte2int(byte[] paramArrayOfByte, int paramInt) {
        return ((0xFF & paramArrayOfByte[(paramInt + 3)]) << 24) +
                ((0xFF & paramArrayOfByte[(paramInt + 2)]) << 16) +
                ((0xFF & paramArrayOfByte[(paramInt + 1)]) << 8) +
                (0xFF & paramArrayOfByte[paramInt]);
    }

    public static byte[] floats2bytes(float[] val) {
        byte[] buf = new byte[val.length * 2];
        for (int i = 0; i < val.length; i++) {
            short data = (short) val[i];
            buf[i * 2] = (byte) (data & 0xff);
            buf[i * 2 + 1] = (byte) ((data >> 8) & 0xff);
        }
        return buf;
    }

    public static float[] bytes2floats(byte[] val) {
        float[] buf = new float[(val.length + 1) / 2];
        for (int i = 0; i < val.length / 2; i++) {
            buf[i] = (val[i * 2] & 0xff) + (val[i * 2 + 1] << 8);
        }
        if (val.length % 2 != 0) {
            buf[buf.length - 1] = val[val.length - 1];
        }
        return buf;
    }

    public static short[] bytes2shorts(byte[] val) {
        short[] buf = new short[(val.length + 1) / 2];
        for (int i = 0; i < val.length / 2; i++) {
            buf[i] = (short) ((val[i * 2] & 0xff) | ((val[i * 2 + 1] & 0xff) << 8));;
        }
        if (val.length % 2 != 0) {
            buf[buf.length - 1] = val[val.length - 1];
        }
        return buf;
    }
}
